#include <gst/video/videooverlay.h>
#include <QApplication>
#include "qtoverlay.h"

PlayerWindow::PlayerWindow(GstElement *p)
    :pipeline(p)
    ,state(GST_STATE_NULL)
    ,totalDuration(GST_CLOCK_TIME_NONE)
{

    videoWindow = new QWidget();

    playerLayout = new QVBoxLayout;
    playerLayout->addWidget(videoWindow);

    this->setLayout(playerLayout);

}

WId PlayerWindow::getVideoWId() const {
    return videoWindow->winId();
}

int main(int argc, char *argv[])
{
    GstElement *pipeline, *source, *h264_caps,*rtpjitterbuffer,*rtph264depay ,*h264parse, *avdec_h264 ,*videoconvert, *sink;
  gst_init (&argc, &argv);
  QApplication app(argc, argv);
  app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit ()));

  // prepare the pipeline

//  GstElement *pipeline = gst_parse_launch ("playbin https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);
//  GstElement *pipeline = gst_parse_launch ("udpsrc port=5004 buffer-size=60000000 ! application/x-rtp, media=video, clock-rate=90000, payload=96, encoding-name=H264 ! rtpjitterbuffer latency=7 ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! autovideosink",NULL);
//  GstElement *pipeline = gst_parse_launch ("udpsrc port=5004 buffer-size=60000000 ! application/x-rtp, media=video, clock-rate=90000, payload=96, encoding-name=H264 ! rtpjitterbuffer latency=7 ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ",NULL);

//gwj
  //  udpsrc port=5004 buffer-size=60000000 ! application/x-rtp, media=video, clock-rate=90000, payload=96, encoding-name=H264 ! rtpjitterbuffer latency=7 ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! autovideosink
    source = gst_element_factory_make ("udpsrc", "source");
    h264_caps = gst_element_factory_make ("capsfilter", "cap_filter");
    rtpjitterbuffer = gst_element_factory_make("rtpjitterbuffer","rtpjitterbuffer");
    rtph264depay = gst_element_factory_make("rtph264depay","rtph264depay");
    h264parse=gst_element_factory_make("h264parse","h264parse");
    avdec_h264=gst_element_factory_make("avdec_h264","avdec_h264");
    videoconvert=gst_element_factory_make("videoconvert","videoconvert");
    sink = gst_element_factory_make ("ximagesink", "sink");

    /* Create the empty pipeline */
    pipeline = gst_pipeline_new ("test-pipeline");

    if (!pipeline || !source || !h264_caps || !sink) {
      g_printerr ("Not all elements could be created.\n");
      return -1;
    }

    /* Build the pipeline */
    gst_bin_add_many (GST_BIN (pipeline), source, h264_caps,rtpjitterbuffer,rtph264depay ,h264parse,avdec_h264 ,videoconvert,sink, NULL);
    if (gst_element_link_many (source, h264_caps, rtpjitterbuffer,rtph264depay ,h264parse,avdec_h264 ,videoconvert,sink, NULL) != TRUE) {
      g_printerr ("Elements could not be linked.\n");
      gst_object_unref (pipeline);
      return -1;
    }

    g_object_set (source, "port", 5004, NULL);
    g_object_set (source, "buffer-size", 60000000, NULL); 

    GstCaps* scalecaps;
    gchar caps_string[]="application/x-rtp, media=video, clock-rate=90000, payload=96, encoding-name=H264";
    scalecaps = gst_caps_from_string(caps_string);
    g_object_set (G_OBJECT (h264_caps), "caps", scalecaps, NULL);

    g_object_set (rtpjitterbuffer, "latency", 7, NULL);

  // prepare the ui
  PlayerWindow *window = new PlayerWindow(pipeline);
  window->resize(900, 600);
  window->show();

  // seg window id to gstreamer
//  GstElement *vsink = gst_element_factory_make ("ximagesink", "vsink");
  WId xwinid = window->getVideoWId();
  gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), xwinid);
//  g_object_set(GST_OBJECT(pipeline), "video-sink", vsink, NULL);

  // connect to interesting signals
//  GstBus *bus = gst_element_get_bus(pipeline);
//  gst_bus_add_watch(bus, &PlayerWindow::postGstMessage, window);
//  gst_object_unref(bus);

  // run the pipeline
  GstStateChangeReturn sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  if (sret == GST_STATE_CHANGE_FAILURE) {
    gst_element_set_state (pipeline, GST_STATE_NULL);
    gst_object_unref (pipeline);
    // Exit application
    QTimer::singleShot(0, QApplication::activeWindow(), SLOT(quit()));
  }

  int ret = app.exec();

  window->hide();
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);

  return ret;
}
